package com.zdsoft.crm.controller.product;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zdsoft.crm.controller.BaseController;
import com.zdsoft.crm.entity.Clue;
import com.zdsoft.crm.entity.product.Product;
import com.zdsoft.crm.service.clue.IClueService;
import com.zdsoft.crm.service.product.IProductService;
import com.zdsoft.crm.util.DateUtil;

@Controller
@RequestMapping("/product/poi")
public class ProductPoiController extends BaseController {

	@Autowired
	IProductService productService;

	@Autowired
	HttpSession session;
	
	@GetMapping("/export")
	public ResponseEntity<byte[]> exportData() throws IOException {
		// 动态获取upload文件夹的路径
		// 生成文件名

		StringBuffer fileName = new StringBuffer(session.getServletContext().getRealPath("/")).append("upload")
				.append(File.separatorChar).append("product").append(File.separatorChar).append("产品")
				.append(DateUtil.parseStr(new Date(), "yyyy年MM月dd日")).append(".xls");
		// 创建对Excel工作簿文件的引用
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 在Excel工作簿中建一工作表，其名为缺省值
		// 如要新建一名为"效益指标"的工作表，其语句为：
		// HSSFSheet sheet = workbook.createSheet("效益指标");
		HSSFSheet sheet = workbook.createSheet("效益指标");
		// 查询数据
		List<Product> ProList = productService.getProAll();
		int i = 0;
		for (Product item : ProList) {
			HSSFRow row = sheet.createRow((short) i);
			HSSFCell cell1 = row.createCell((short) 0);
			// 定义单元格为字符串类型
			cell1.setCellValue(item.getId());

			HSSFCell cell2 = row.createCell((short) 1);
			// 定义单元格为字符串类型
			cell2.setCellValue(item.getProductName());

			HSSFCell cell3 = row.createCell((short) 2);
			// 定义单元格为字符串类型
			cell3.setCellValue(item.getProduct_type_id());

			HSSFCell cell4 = row.createCell((short) 3);
			// 定义单元格为字符串类型
			cell4.setCellValue(item.getCreate_time());

			HSSFCell cell5 = row.createCell((short) 4);
			// 定义单元格为字符串类型
			cell5.setCellValue(item.getProduct_URL());

			HSSFCell cell6 = row.createCell((short) 5);
			// 定义单元格为字符串类型
			cell6.setCellValue(item.getTeam());

			HSSFCell cell7 = row.createCell((short) 6);
			// 定义单元格为字符串类型
			cell7.setCellValue(item.getCost_price());

			HSSFCell cell8 = row.createCell((short) 7);
			// 定义单元格为字符串类型
			cell8.setCellValue(item.getList_price());

			HSSFCell cell9 = row.createCell((short) 8);
			// 定义单元格为字符串类型
			cell9.setCellValue(item.getMaster_map());

			HSSFCell cell10 = row.createCell((short) 9);
			// 定义单元格为字符串类型
			cell10.setCellValue(item.getProduct_info());

			HSSFCell cell11 = row.createCell((short) 10);
			// 定义单元格为字符串类型
			cell11.setCellValue(item.getPicture());

			i++;
		}
		// 新建一输出文件流
		File file = new File(fileName.toString());
		if (!file.exists())
			file.createNewFile();
		workbook.write(file);

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", file.getName(), Charset.forName("UTF-8"));// 防止文件名乱码
		HttpStatus statusCode = HttpStatus.OK;
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers,
				statusCode);
		return entity;
	}

}
